package module

import (
    "github.com/gin-gonic/gin"
    "strconv"
    "yi-shou-backstage/errmsg"
    "yi-shou-backstage/model"
    "yi-shou-backstage/module/param"
    "yi-shou-backstage/module/result"
    "yi-shou-backstage/util"
)

func CreateArticleControl(req *gin.Context) (int, string, interface{}) {
    var params model.Article
    // 获取参数
    draft, err := strconv.ParseBool(req.Query("draft"))
    if err != nil {
        util.LogEntry.Errorf("数据类型转换错误, error = %s \n", err)
        return errmsg.DATA_TYPE_CONV_ERROR, errmsg.GetErrorByCode(errmsg.DATA_TYPE_CONV_ERROR), nil
    }
    err = req.ShouldBindJSON(&params)
    if err != nil {
        util.LogEntry.Errorf("参数格式错误, error = %s, param = %s \n", err, params)
        return errmsg.PARAM_FORMAT_ERROR, errmsg.GetErrorByCode(errmsg.PARAM_FORMAT_ERROR), nil
    }

    // 校验参数
    if ok, msg := util.Validator(&params); !ok {
        util.LogEntry.Errorf("数据校验失败, error = %s, param = %s \n", err, params)
        return errmsg.VALIDATE_ERROR, msg, nil
    }

    params.Draft = draft
    // 保存数据
    data := model.CreateArticle(params)
    if data == nil {
        util.LogEntry.Errorf("发布文章失败 \n")
        return errmsg.CREATE_ARTICLE_ERROR, errmsg.GetErrorByCode(errmsg.CREATE_ARTICLE_ERROR), nil
    }
    return errmsg.SUCCESS, errmsg.GetErrorByCode(errmsg.SUCCESS), data
}

// 修改文章
func UpdateArticleControl(req *gin.Context) (int, string, interface{}) {
    // 获取参数
    id, err := strconv.Atoi(req.Param("id"))
    draft, err := strconv.ParseBool(req.Query("draft"))
    if err != nil {
        util.LogEntry.Errorf("数据类型转换错误, error = %s \n", err)
        return errmsg.DATA_TYPE_CONV_ERROR, errmsg.GetErrorByCode(errmsg.DATA_TYPE_CONV_ERROR), nil
    }
    var params model.Article
    err = req.ShouldBindJSON(&params)
    if err != nil {
        util.LogEntry.Errorf("参数格式错误, error = %s, param = %s \n", err, params)
        return errmsg.PARAM_FORMAT_ERROR, errmsg.GetErrorByCode(errmsg.PARAM_FORMAT_ERROR), nil
    }

    // 检验参数
    if ok, msg := util.Validator(&params); !ok {
        util.LogEntry.Errorf("数据校验失败, error = %s, param = %s \n", err, params)
        return errmsg.VALIDATE_ERROR, msg, nil
    }

    // 保存数据
    params.Draft = draft
    // 保存数据
    data := model.UpdateArticle(id, params)
    if data == nil {
        util.LogEntry.Errorf("发布文章失败 \n")
        return errmsg.CREATE_ARTICLE_ERROR, errmsg.GetErrorByCode(errmsg.CREATE_ARTICLE_ERROR), nil
    }
    return errmsg.SUCCESS, errmsg.GetErrorByCode(errmsg.SUCCESS), data
}

// 获取指定的文章
func GetArticleControl(req *gin.Context) (int, string, interface{}) {
    // 获取参数
    id, err := strconv.Atoi(req.Param("id"))
    if err != nil {
        util.LogEntry.Errorf("数据类型转换错误, error = %s \n", err)
        return errmsg.DATA_TYPE_CONV_ERROR, errmsg.GetErrorByCode(errmsg.DATA_TYPE_CONV_ERROR), nil
    }

    // 获取数据
    data := model.GetArticle(id)
    if data == nil {
        util.LogEntry.Errorf("该篇文章不存在，id = %d \n")
        return errmsg.ARTICLE_NOT_EXIST, errmsg.GetErrorByCode(errmsg.ARTICLE_NOT_EXIST), nil
    }
    return errmsg.SUCCESS, errmsg.GetErrorByCode(errmsg.SUCCESS), data
}

// 获取文章列表
func ListArticlesControl(req *gin.Context) (int, string, interface{}) {
    // 获取参数
    //err := req.ShouldBindQuery(&params)
    page, err := strconv.Atoi(req.DefaultQuery("page", "-1"))
    per_page, err := strconv.Atoi(req.DefaultQuery("per_page", "-1"))
    status, err := strconv.Atoi(req.DefaultQuery("status", "0"))
    channel_id, err := strconv.Atoi(req.DefaultQuery("channel_id", "0"))
    //loc, _ := time.LoadLocation("Asia/Shanghai")
    //publish_start, err := time.ParseInLocation("2006-01-02 15:04:05", req.DefaultQuery("begin_pubdate", "0001-01-01 00:00:00"), loc)
    //publish_end, err := time.ParseInLocation("2006-01-02 15:04:05", req.DefaultQuery("end_pubdate", "0001-01-01 00:00:00"), loc)
    publish_start := req.DefaultQuery("begin_pubdate", "")
    publish_end := req.DefaultQuery("end_pubdate", "")
    if err != nil {
        util.LogEntry.Errorf("参数格式错误, error = %s \n", err)
        return errmsg.PARAM_FORMAT_ERROR, errmsg.GetErrorByCode(errmsg.PARAM_FORMAT_ERROR), nil
    }
    params := param.ListArticlesParam{
        Page: page,
        PerPage: per_page,
        Status: status,
        ChannelID: channel_id,
        PublishStart: publish_start,
        PublishEnd: publish_end,
    }

    // 根据条件查询文章
    total, rows := model.ListArticles(params)
    data := result.ListArticlesResponse{TotalCount: total, Page: params.Page, PerPage: params.PerPage, Results: rows}
    return errmsg.SUCCESS, errmsg.GetErrorByCode(errmsg.SUCCESS), data
}

// 删除文章
func DeleteArticleControl(req *gin.Context) (int, string, interface{}) {
    // 获取参数
    id, err := strconv.Atoi(req.Param("id"))
    if err != nil {
        util.LogEntry.Errorf("参数格式错误, error = %s \n", err)
        return errmsg.PARAM_FORMAT_ERROR, errmsg.GetErrorByCode(errmsg.PARAM_FORMAT_ERROR), nil
    }

    // 删除文章
    data := model.DeleteArticle(id)
    if data == nil {
        util.LogEntry.Errorf("删除文章失败，article_id = %d \n", id)
        return errmsg.DELETE_ARTICLE_ERROR, errmsg.GetErrorByCode(errmsg.DELETE_ARTICLE_ERROR), nil
    }
    return errmsg.SUCCESS, errmsg.GetErrorByCode(errmsg.SUCCESS), data
}